package com.brspace.qualitycontrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brspace.qualitycontrol.entity.Prospecting;
import com.brspace.qualitycontrol.entity.vo.ProspectQueryVo;
import com.brspace.qualitycontrol.entity.vo.ProspectingVo;
import com.brspace.qualitycontrol.entity.vo.ProspertFangVo;
import com.brspace.qualitycontrol.entity.vo.ProspertListVo;
import com.brspace.qualitycontrol.mapper.ProspectingMapper;
import com.brspace.qualitycontrol.service.ProspectingService;
import com.brspace.qualitycontrol.utils.EmptyUtil;
import com.brspace.qualitycontrol.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 选址勘探 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-04-12
 */
@Service
public class ProspectingServiceImpl extends ServiceImpl<ProspectingMapper, Prospecting> implements ProspectingService {
    @Resource
    private ProspectingMapper prospectingMapper;

    @Override
    public R selectPage01(Long page, Long limit, ProspectQueryVo prospectQueryVo) {
        List<ProspertListVo>  list = new ArrayList<>();


        QueryWrapper<ProspectingVo> queryWrapper = new QueryWrapper<>();
        String name = prospectQueryVo.getName();
        String address = prospectQueryVo.getAddress();
        String townTimeBig = prospectQueryVo.getTownTimeBig();
        String townTimeEnd = prospectQueryVo.getTownTimeEnd();
        if (!EmptyUtil.isEmpty(name)){
            queryWrapper.eq("h.name",name);
        }
        if (!EmptyUtil.isEmpty(address)){
            queryWrapper.like("h.address",address);
        }
        if (!EmptyUtil.isEmpty(townTimeBig)) {
            queryWrapper.ge("h.town_time",townTimeBig);
        }
        if (!EmptyUtil.isEmpty(townTimeEnd)) {
            queryWrapper.le("h.town_time",townTimeEnd);
        }
        Page<ProspectingVo> pageParam = new Page<>(page, limit);
        //放入分页参数和查询条件参数，mp会自动组装
        List<ProspectingVo> records = prospectingMapper.selectPageByCourseQueryVo(pageParam, queryWrapper);
        for (ProspectingVo record : records) {
                List<ProspertFangVo>  listFang = new ArrayList<>();
                ProspertListVo listVo = new ProspertListVo();
                 // 姓名
                listVo.setName(record.getName());
                // 家庭住址
                listVo.setAddress(record.getAddress());
                // 选址
                listVo.setAddWei(record.getAddWei());
                // 审批时间
                listVo.setTownTime(record.getTownTime());
                // 勘探事件
                listVo.setSceneTime(record.getSceneTime());
                // 勘探人员
                listVo.setSceneUser(record.getSceneUser());
                // 方位
                ProspertFangVo fangVo01 = new ProspertFangVo();
                fangVo01.setValue("东至");
                fangVo01.setName(record.getEast());
                listFang.add(fangVo01);
                ProspertFangVo fangVo02 = new ProspertFangVo();
                fangVo02.setValue("南至");
                fangVo02.setName(record.getSouth());
                listFang.add(fangVo02);
                ProspertFangVo fangVo03 = new ProspertFangVo();
                fangVo03.setValue("西至");
                fangVo03.setName(record.getWest());
                listFang.add(fangVo03);
                ProspertFangVo fangVo04 = new ProspertFangVo();
                fangVo04.setValue("北至");
                fangVo04.setName(record.getNorth());
                listFang.add(fangVo04);
                listVo.setProspertFangList(listFang);
                // 定点放样
                listVo.setLofting(record.getLofting());
                // 定点放样事件
                listVo.setLoftingTime(record.getLoftingTime());
                // 砌基
                listVo.setMasonry(record.getMasonry());
                // 砌基事件
                listVo.setMasonryTime(record.getMasonryTime());
                // 完成
                listVo.setFinishing(record.getFinishing());
                // 完成时间
                listVo.setFinishTime(record.getFinishTime());
                // 状态
                listVo.setState(record.getState());
                list.add(listVo);
        }
        return R.ok().data("rows",list).data("total",pageParam.getTotal());
    }

    @Override
    public IPage<Prospecting> stateList(Page<Prospecting> pageParam, ProspectQueryVo prospectingVo) {
        System.err.println(prospectingVo);
        QueryWrapper<Prospecting> queryWrapper = new QueryWrapper<>();
        if (prospectingVo==null){
            return baseMapper.selectPage(pageParam,queryWrapper);
        }
        String name = prospectingVo.getName();
        String address =  prospectingVo.getAddress();
        String townTimeBig = prospectingVo.getTownTimeBig();
        String townTimeEnd = prospectingVo.getTownTimeEnd();
        if (!EmptyUtil.isEmpty(name)){
            queryWrapper.eq("house_name",name);
        }
        if (!EmptyUtil.isEmpty(address)){
            queryWrapper.like("address",address);
        }
        if (!EmptyUtil.isEmpty(townTimeBig)) {
            queryWrapper.ge("town_time",townTimeBig);
        }
        if (!EmptyUtil.isEmpty(townTimeEnd)) {
            queryWrapper.le("town_time",townTimeEnd);
        }
        return baseMapper.selectPage(pageParam,queryWrapper);
    }
}
